package cn.minimelon.bittermelon.utils;

import cn.minimelon.bittermelon.bean.AjaxResult;
import cn.minimelon.bittermelon.bean.BaseEntity;
import cn.minimelon.bittermelon.bean.LoginUser;
import cn.minimelon.bittermelon.bean.PageReq;
import org.beetl.sql.core.page.DefaultPageRequest;
import org.beetl.sql.core.page.PageRequest;

import java.util.Collection;
import java.util.Date;

public class Melon {
    public final static String LOGIN_USER_SESSION = "loginUser";
    public final static int SUCCESS_CODE = 1;
    public final static int ERROR_CODE = 0;

    public static AjaxResult<Object> success(String msg, Object data) {
        AjaxResult<Object> r = new AjaxResult<>();
        r.setCode(SUCCESS_CODE);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> AjaxResult<T> success(T data) {
        AjaxResult<T> r = new AjaxResult<>();
        r.setCode(SUCCESS_CODE);
        r.setMsg("success");
        r.setData(data);
        return r;
    }

    public static AjaxResult<Object> success() {
        return success("success", null);
    }

    public static AjaxResult<Object> error(String msg) {
        AjaxResult<Object> r = new AjaxResult<>();
        r.setCode(ERROR_CODE);
        r.setMsg(msg);
        return r;
    }

    public static PageRequest PR(PageReq req) {
        if (req.getPage() != null && req.getPageSize() != null) {
            return DefaultPageRequest.of(req.getPage(), req.getPageSize());
        } else {
            return DefaultPageRequest.of(1, 20);
        }
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> collection) {
        return collection != null && !collection.isEmpty();
    }

    public static void setInsertInfo(BaseEntity entity, LoginUser user) {
        // entity.setTenantId(user.);
        entity.setVipLevel(user.getAuthLevel());
        entity.setCreatedBy(user.getCode());
        entity.setCreatedTime(new Date());
        setUpdateInfo(entity, user);
    }

    public static void setUpdateInfo(BaseEntity entity, LoginUser user) {
        entity.setUpdatedBy(user.getCode());
        entity.setUpdatedTime(new Date());
    }
}
